package com.edu.exam.enums;

/**
 * 考试形式枚举
 * 
 * @author S-01 Module Designer
 * @version 2.0
 * @since 2025-11-10
 */
public enum ExamForm {
    WRITTEN("written", "笔试"),
    ORAL("oral", "口试"),
    PRACTICAL("practical", "实践考试"),
    ONLINE("online", "在线考试"),
    MIXED("mixed", "混合形式");

    private final String code;
    private final String description;

    ExamForm(String code, String description) {
        this.code = code;
        this.description = description;
    }

    public String getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 从代码创建枚举实例
     *
     * @param code 枚举代码
     * @return 对应的枚举实例
     * @throws IllegalArgumentException 如果代码无效
     */
    public static ExamForm fromCode(String code) {
        if (code == null || code.trim().isEmpty()) {
            throw new IllegalArgumentException("Code cannot be null or empty");
        }
        for (ExamForm form : values()) {
            if (form.getCode().equals(code)) {
                return form;
            }
        }
        throw new IllegalArgumentException("Unknown code: " + code);
    }

    /**
     * 从描述创建枚举实例
     *
     * @param description 枚举描述
     * @return 对应的枚举实例
     * @throws IllegalArgumentException 如果描述无效
     */
    public static ExamForm fromDescription(String description) {
        if (description == null || description.trim().isEmpty()) {
            throw new IllegalArgumentException("Description cannot be null or empty");
        }
        for (ExamForm form : values()) {
            if (form.getDescription().equals(description)) {
                return form;
            }
        }
        throw new IllegalArgumentException("Unknown description: " + description);
    }

    @Override
    public String toString() {
        return description;
    }
}